import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Thread5 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        long stime=System.currentTimeMillis();
        int[] arr=new int[10000000];
        Random random=new Random();
        for(int i=0;i<arr.length;i++){
            int temp=random.nextInt(100)+1;
            arr[i]=temp;
        }
        FutureTask<Integer> task1=new FutureTask<Integer>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int temp=0;
                for (int i = 0; i < arr.length; i=i+2) {
                    temp+=arr[i];
                }
                System.out.println("线程1："+temp);
                return temp;
            }
        });
        Thread t1=new Thread(task1);
        t1.start();
        FutureTask<Integer> task2=new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int temp=0;
                for (int i = 1; i <arr.length ; i=i+2) {
                    temp+=arr[i];
                }
                System.out.println("线程2："+temp);
                return temp;
            }
        });
        Thread t2=new Thread(task2);
        t2.start();
        int sum= task1.get()+task2.get();
        System.out.println("最终结果"+sum);
        long etime=System.currentTimeMillis();
        System.out.println("最终执行时间"+(etime-stime));
    }
}
